package com.wchen.gdoc.generate.util;

import com.wchen.gdoc.generate.resource.structure.*;

import java.util.List;

/**
 * @Date: 2019/7/9 22:25
 * @Auther: wchen
 * @Version: 1.0.0
 * @Description:
 */
public class StructureUtil {

    /**
     * 返回包含某个注解类型的注解
     * @param annotationStructures
     * @param annotationType
     * @return
     */
    public static String getAssignAnnotationType(List<AnnotationStructure> annotationStructures, String annotationType){
        for (AnnotationStructure annotationStructure : annotationStructures) {
            if(annotationStructure.getElementType().contains(annotationType)){
                return annotationStructure.getType();
            }
        }
        return null;
    }



    /**
     * 是否包含某个注解
     * @param annotationStructures
     * @param annotationType
     * @return
     */
    public static Boolean containsAnnotation(List<AnnotationStructure> annotationStructures, String annotationType){
        for (AnnotationStructure annotationStructure : annotationStructures) {
            if(annotationStructure.getElementType().contains(annotationType)){
                return Boolean.TRUE;
            }
        }
        return Boolean.FALSE;
    }

    /**
     * 取某个注解类型的值
     * @param annotationStructures
     * @param annotationType
     * @param key
     * @return
     */
    public static String getAnnotationValue(List<AnnotationStructure> annotationStructures, String annotationType, String key){
        for (AnnotationStructure annotationStructure : annotationStructures) {
            if(annotationStructure.getElementType().contains(annotationType)){
                return annotationStructure.getElementMap().get(key);
            }
        }
        return null;
    }


    /**
     * 获取特定类型的注释
     * @param objectStructure
     * @return
     */
    public static String getCommentText(ObjectStructure objectStructure){
        String commentText = objectStructure.getCommentText();
        if(objectStructure instanceof ParameterStructure){
            commentText = ((ParameterStructure) objectStructure).getParamCommentText();
        }else if(objectStructure instanceof FieldStructure){
            commentText = ((FieldStructure) objectStructure).getFieldCommentText();
        }else if(objectStructure instanceof ReturnedStructure){
            commentText = ((ReturnedStructure) objectStructure).getReturnedCommentText();
        }

        return commentText;
    }



    /**
     * 获取类型的命名
     * @param objectStructure
     * @return
     */
    public static String getTypeAlias(ObjectStructure objectStructure){
        String alias = objectStructure.getSimpleName();
        if(objectStructure instanceof ParameterStructure){
            alias = ((ParameterStructure) objectStructure).getParamName();
        }else if(objectStructure instanceof FieldStructure){
            alias = ((FieldStructure) objectStructure).getFieldName();
        }

        return alias;
    }




}
